# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import csv

class JdscPipeline:
    def open_spider(self, spider):
        if spider.name == 'dcjd':
            self.f = open('./jd_data.csv', 'w', newline='', encoding='utf-8')
            self.write = csv.writer(self.f, dialect=('excel'))
            self.write.writerow(['名称', '店铺', '价格', '评论数', '链接'])

    def close_spider(self, spider):
        if spider.name == 'dcjd':
            self.f.close()

    def process_item(self, item, spider):
        if spider.name == 'dcjd':
            self.write.writerow([item['name'], item['shop'], item['price'], item['reply'], item['link']])
        return item
